/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers.courses;

import controllers.utils.DatabaseHjelper;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import models.Course;
import models.Department;
import models.Institute;
import models.StudyProgram;

/**
 *
 * @author dybvik
 */
@SessionScoped
@Named(value="newCourse")
public class AddNewCourse implements Serializable{
    private Course newCourse = new Course();
    private final DatabaseHjelper dbh = DatabaseHjelper.hent();
    private final List<StudyProgram> studprogs = new ArrayList<StudyProgram>();

    /** Creates a new instance of AddNewCourse */
    public AddNewCourse() {
    }
    
    public String addNewCourse(){
        newCourse.setCourseCode(null);
        newCourse.setCourseName(null);
        newCourse.setCourseLevel(null);
        newCourse.setCourseCredit(0);
        newCourse.setCredOverlapReduction(0);
        newCourse.setSemester(null);
        newCourse.setTeachingLanguage(null);
        newCourse.setPrerequisites(null);
        newCourse.setCourseContent(null);
        newCourse.setPedMethods(null);
        newCourse.setAssessment(null);
        newCourse.setGradingScale(null);
        newCourse.setUseOfExaminers(null);
        newCourse.setReqResitExam(null);
        newCourse.setExamAids(null);
        newCourse.setMandetoryWorkReq(null);
        newCourse.setReadingList(null);
        newCourse.setReplacesCourse(null);
        newCourse.setAdditionalNotes(null);
        newCourse.setStudyprogram(new StudyProgram());
        newCourse.getStudyprogram().setDepartment(new Department());
        newCourse.getStudyprogram().getDepartment().setInstitute((new Institute()));
         return "addNewCourse";
    }

    public String executeNewCourse(){
        addNewCourseToDB();
        return "addNewCourseOutput";
    }

    private void addNewCourseToDB() {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = dbh.finnForbindelse();
            stmt = conn.prepareStatement("INSERT INTO course (courseCode, courseName, courseLevel, courseCredits, credOverlapReduction, semester, teachingLanguage, prerequisites, courseContent, pedMethods, assessment, gradingScale, useOfExaminers, reqResitExam, examAids, mandetoryWorkReq, readingList, replacesCourse, additionalNotes) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            stmt.setString(1, newCourse.getCourseCode());
            stmt.setString(2, newCourse.getCourseName());
            stmt.setString(3, newCourse.getCourseLevel());
            stmt.setInt(4, newCourse.getCourseCredit());
            stmt.setInt(5, newCourse.getCredOverlapReduction());
            stmt.setString(6, newCourse.getSemester());
            stmt.setString(7, newCourse.getTeachingLanguage());
            stmt.setString(8, newCourse.getPrerequisites());
            stmt.setString(9, newCourse.getCourseContent());
            stmt.setString(10, newCourse.getPedMethods());
            stmt.setString(11, newCourse.getAssessment());
            stmt.setString(12, newCourse.getGradingScale());
            stmt.setString(13, newCourse.getUseOfExaminers());
            stmt.setString(14, newCourse.getReqResitExam());
            stmt.setString(15, newCourse.getExamAids());
            stmt.setString(16, newCourse.getMandetoryWorkReq());
            stmt.setString(17, newCourse.getReadingList());
            stmt.setString(18, newCourse.getReplacesCourse());
            stmt.setString(19, newCourse.getAdditionalNotes());
            stmt.executeUpdate();
            /*
            stmt = conn.prepareStatement("INSERT INTO StudyProgramCourse(courseID, studyProgramID) values(?, ?)");
            stmt.setInt(1,newCourse.getCourseID());
            stmt.setInt(2, newCourse.getStudyprogram().getStudyProgramID());
            stmt.executeUpdate();
             * 
             */
        } catch (SQLException e) {
            System.out.println("Feil i lagring av Course: " + e);
        } finally {
            DatabaseHjelper.lukk(stmt);
            DatabaseHjelper.lukk(conn);
        }
    }

    private void updateStudyProgram() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet res = null;
        try {
            conn = dbh.finnForbindelse();
            stmt = conn.prepareStatement("select * from studyprogram");
            res = stmt.executeQuery();
            synchronized (studprogs) {
                studprogs.clear();
                while (res.next()){
                    StudyProgram sp = new StudyProgram();
                    sp.setStudyProgramID(res.getInt("studyProgramID"));
                    sp.setStudyProgramCode(res.getInt("studyProgramCode"));
                    sp.setName(res.getString("studyProgramName"));
                    sp.getDepartment().setDepartmentID(res.getInt("departmentID"));
                    sp.setApproved(res.getDate("approved"));
                    sp.setLastRevised(res.getDate("lastRevised"));
                    sp.setIsNKRFromInt(res.getInt("isNKR"));
                    sp.setApprovedBy(res.getString("approvedBy"));
                    studprogs.add(sp);
                }
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            DatabaseHjelper.lukk(res);
            DatabaseHjelper.lukk(stmt);
            DatabaseHjelper.lukk(conn);
        }
    }

    public Course getNewCourse() {
        return newCourse;
    }

    public List<StudyProgram> getStudprogs() {
        updateStudyProgram();
        return studprogs;
    }

}
